package com.tools.compass;

import android.app.Activity;
import android.app.Service;
import android.appwidget.AppWidgetManager;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.RemoteViews;

import com.tools.compass.util.Constants;
import com.tools.compass.util.SharedPref;
import com.umeng.analytics.MobclickAgent;

public class WidgetConfigureActivity extends Activity implements
		OnItemClickListener {
	GridView widget_config;
	int mAppWidgetId = AppWidgetManager.INVALID_APPWIDGET_ID;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setResult(RESULT_CANCELED);
		setContentView(R.layout.widget_config);

		Intent intent = getIntent();
		Bundle extras = intent.getExtras();
		if (extras != null) {
			mAppWidgetId = extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID,
					AppWidgetManager.INVALID_APPWIDGET_ID);
		}

		widget_config = (GridView) findViewById(R.id.widget_config);
		widget_config.setAdapter(new GridAdapter(this.getApplicationContext()));
		widget_config.setOnItemClickListener(this);

		int position = SharedPref.getInstanse(this.getApplicationContext())
				.getWidget(0);
		widget_config.setSelection(position);

		// If they gave us an intent without the widget id, just bail.
		if (mAppWidgetId == AppWidgetManager.INVALID_APPWIDGET_ID) {
			finish();
		}
	}

	@Override
	protected void onResume() {
		super.onResume();
		MobclickAgent.onResume(this);
	}

	@Override
	protected void onPause() {
		super.onPause();
		MobclickAgent.onPause(this);
	}

	class GridAdapter extends BaseAdapter {
		Context mContext;

		public GridAdapter(Context context) {
			mContext = context;
		}

		@Override
		public int getCount() {
			return Constants.compasses.length;
		}

		@Override
		public Object getItem(int position) {
			if (position < 0 || position > Constants.compasses.length - 1) {
				return null;
			}
			return Constants.compasses[position];
		}

		@Override
		public long getItemId(int position) {
			return 0;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			ViewHoder hoder;
			if (convertView == null) {
				hoder = new ViewHoder();
				LayoutInflater inflater = (LayoutInflater) mContext
						.getSystemService(Service.LAYOUT_INFLATER_SERVICE);
				convertView = inflater.inflate(
						R.layout.widget_config_grid_item, null);
				hoder.image = (ImageView) convertView.findViewById(R.id.logo);
				convertView.setTag(hoder);
			} else {
				hoder = (ViewHoder) convertView.getTag();
			}
			hoder.image.setImageResource(Constants.compasses[position]);
			return convertView;
		}

		class ViewHoder {
			ImageView image;
		}
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id) {
		if (position >= 0 && position <= Constants.compasses.length - 1) {
			SharedPref.getInstanse(this.getApplicationContext()).putWidget(
					position);
		}
		AppWidgetManager manager = AppWidgetManager.getInstance(this
				.getApplicationContext());
		manager.updateAppWidget(new ComponentName(this.getPackageName(),
				CompassWidget.class.getName()),
				new RemoteViews(this.getPackageName(), R.layout.compass_widget));

		Intent resultValue = new Intent();
		resultValue.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, mAppWidgetId);
		setResult(RESULT_OK, resultValue);
		finish();
	}
}
